package com.dap.cms.governance.controller;

import com.dap.dao.model.QueryResult;
import com.dap.cms.governance.po.CmsComponentUserPo;
import com.dap.cms.governance.service.CmsComponentUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fesine.commons.entity.Result;
import com.fesine.commons.enums.ResultEnum;
import com.fesine.commons.util.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: t_cms_component_user表对应的控制层类
 * @author: Fesine
 * @createTime:2018/08/21
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2018/08/21
 */
@RestController
@RequestMapping("/v1")
public class CmsComponentUserController {
    private static final Logger logger=LoggerFactory.getLogger(CmsComponentUserController.class);
    @Autowired
    private CmsComponentUserService cmsComponentUserService;

    /**
     * 新增CmsComponentUserPo
     * @param cmsComponentUserPo
     * @return result
     */
    @PostMapping("/cmsComponentUser")
    public Result addCmsComponentUser(CmsComponentUserPo cmsComponentUserPo){
        try{
            cmsComponentUserService.save(cmsComponentUserPo);
            return ResultUtils.success(ResultEnum.CREATED,null);
        }catch(Exception e){
            logger.error("新增失败",e);
            return ResultUtils.error(500,"新增失败");
        }

    }

    /**
     * 更新CmsComponentUserPo
     * @param cmsComponentUserPo
     * @return result
     */
    @PutMapping("/cmsComponentUser")
    public Result updateCmsComponentUser(CmsComponentUserPo cmsComponentUserPo){
        try{
            cmsComponentUserService.update(cmsComponentUserPo);
            return ResultUtils.success(ResultEnum.CREATED,null);
        }catch(Exception e){
            logger.error("更新失败",e);
            return ResultUtils.error(500,"更新失败");
        }

    }

    /**
     * 删除CmsComponentUserPo
     * @param id
     * @return result
     */
    @DeleteMapping("/cmsComponentUser/{id}")
    public Result deleteCmsComponentUser(@PathVariable("id") Integer id){
        CmsComponentUserPo cmsComponentUserPo=new CmsComponentUserPo();
        cmsComponentUserPo.setId(id);
        cmsComponentUserService.delete(cmsComponentUserPo);
        return ResultUtils.success(cmsComponentUserPo);
    }

    /**
     * 查看CmsComponentUserPo详情
     * @param id
     * @return result
     */
    @GetMapping("/cmsComponentUser/{id}")
    public Result getCmsComponentUser(@PathVariable("id") Integer id){
        CmsComponentUserPo cmsComponentUserPo=new CmsComponentUserPo();
        cmsComponentUserPo.setId(id);
        cmsComponentUserPo=cmsComponentUserService.get(cmsComponentUserPo);
        return ResultUtils.success(cmsComponentUserPo);
    }

    /**
     * 查询CmsComponentUserPo列表
     * @param cmsComponentUserPo
     * @return result
     */
    @GetMapping("/cmsComponentUsers")
    public Result listAll(CmsComponentUserPo cmsComponentUserPo){
        List<CmsComponentUserPo> list=cmsComponentUserService.listAll(cmsComponentUserPo);
        return ResultUtils.success(list);
    }

    /**
     * 分页查询CmsComponentUserPo列表
     * @param cmsComponentUserPo
     * @param pageIndex
     * @param pageSize
     * @return result
     */
    @GetMapping("/cmsComponentUsers/page")
    public Result listPage(CmsComponentUserPo cmsComponentUserPo,@RequestParam(value = "pageIndex",
        required = false, defaultValue = "1")
            Integer pageIndex,@RequestParam(value = "pageSize", required = false, defaultValue =
        "10") Integer pageSize){
        QueryResult<CmsComponentUserPo> result=cmsComponentUserService.listPage(cmsComponentUserPo,pageIndex,pageSize);
        long total=result.getTotalrecord();
        Map<String, Object> map=new HashMap(16);
        if(total > 0){
            map.put("totalRecord",total);
            map.put("resultList",result.getResultlist());
        }
        return ResultUtils.success(map);
    }

}

